#include <extractGraph/extractgraph.h>
#include <multipleImage/multipleImage.h>
#include <opencv2/xfeatures2d/nonfree.hpp>

using namespace xfeatures2d;


int main()
{
    //源图
    ExtractGraph extractImg;
    Mat srcImg = imread("../../resource/image/circult/image2.jpg");
    Mat templateImg = imread("../../resource/image/base/Diode.jpg");

    Mat srcComponet = extractImg.extractComponet(srcImg);
    Mat templateCompoent = extractImg.extractComponet(templateImg);
    //////////轮廓检测////////////
//    vector< vector<Point> > contoursData;
//    Mat contoursImg = srcComponet.clone();
//    findContours(contoursImg, contoursData, noArray(), RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

//    contoursImg = Scalar::all(0);
//    drawContours(contoursImg, contoursData, -1, Scalar::all(255));

    /////////模板匹配////////
    Mat result;
    matchTemplate(srcComponet, templateCompoent, result, TM_CCOEFF_NORMED);//模板匹配
    double maxVal, minVal;
    Point minLoc, maxLoc;
    //寻找匹配结果中的最大值和最小值以及坐标位置
    minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);
    //绘制最佳匹配区域
    Mat matchResult = srcComponet.clone();
    rectangle(matchResult,cv::Rect(maxLoc.x,maxLoc.y,templateCompoent.cols,templateCompoent.rows),Scalar(255),2);
    //显示
    vector<Mat> imgShowVector;
    imgShowVector.push_back(extractImg.extractBlueLayer(srcImg));
    imgShowVector.push_back(srcComponet);
    imgShowVector.push_back(matchResult);
    Mat resultImg;
    multipleImage(imgShowVector, resultImg, 3, 0.7);
    imshow("结果", resultImg);

//    imshow("原图", srcComponet);
//    imshow("匹配", matchResult);
    imshow("模板图像", templateCompoent);
//    imshow("result", result);

    waitKey(0);
    destroyAllWindows();


    return 0;
}
